<?php 
/************************************************************************************************
*   This file is a part of JV-LinkExchanger, a link exchange component for Joomla
*   Copyright (C) 2007-2008  Vijaykumar Niranjan 
*	
*   This program is free software; you can redistribute it and/or modify 
*   it under the terms of the GNU General Public License as published by 
*   the Free Software Foundation; either version 2 of the License, or 
*  (at your option) any later version. 
*
*   This program is distributed in the hope that it will be useful, 
*   but WITHOUT ANY WARRANTY; without even the implied warranty of 
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
*   GNU General Public License for more details. 
*
*   You should have received a copy of the GNU General Public License 
*   along with this program; if not, write to the Free Software 
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

* 	JV-LinkExchanger - Link Exchange Component for Joomla 1.0.x
* 	@package JV-LinkExchanger
* 	@file version 1.2.8 RELEASE 
* 	@author Vijaykumar Niranjan
* 	@link http://www.jv-extensions.com
* 	@copyright (C) 2007-2008 by Vijaykumar Niranjan
* 	@license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*  	This program is distributed with the hope that it will be useful,
*  	but WITHOUT ANY WARRANTY; without even the implied warranty of
*  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
***************************************************************************************************/
// ensure this file is being included by a parent file  
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.'); 

function com_install()
{
	global $database, $mosConfig_absolute_path;
	/** XML file has been used for installing the component. 
	* Now its time to perform some post installation tasks
	*/
	$act_newinstall = 0;
	require_once($mosConfig_absolute_path."/administrator/components/com_jvlinx/jvlinx.postinstall.php");
			
	echo '<h3><b>JV-BE Installation completed successfully...</b></h3><br/><hr/>';
	echo '<h3><b>Starting JV-BE Post-Installation tasks...</b></h3><br />';

	// Get Database version
	$database->setQuery("select `version` from `#__jvlinx_settings`");
	$db_vers = ($database->query()) ? trim($database->loadResult()) : 0;
	echo 'JV-BE Version in Database: '.$db_vers.'<br />';

	if (!$db_vers)
	{
		// Possibly database has some other field (< v1.2.8). So check for some other field as well
		$database->setQuery("select `site_title` from `#__jvlinx_settings`");
		$tmp1 = ($database->query()) ? trim($database->loadResult()) : "";
		if ($tmp1 == "")
			$act_newinstall = 1;	
		else
			$db_vers = 127; // auto upgrade starts from this version (v128)		
	}
			
	// Get XML version
	$fp = @fopen($mosConfig_absolute_path."/administrator/components/com_jvlinx/VERSION.txt", "r");
	$data = fgets($fp);				
	fclose($fp);		
	$ts = strpos($data, "XML-VERSION=") + 12; 
	$xml_vers = str_replace(".","",trim(substr($data, $ts)));	
	echo 'JV-BE Version in Files: '.$xml_vers.'<br /><br/>';	
				
	// Fresh install
	$retmsg = "";
	if ($act_newinstall)
	{
		echo 'Initiating a new database installation for JV-BE...<br />';
		if (!Jvlinx_PostInstall::execNewinstall($retmsg))
		{
			echo $retmsg . 'Error during post installation<br />';
			echo 'Post installation task failed. Please contact support@www.jv-extensions.com';
			return false;
		}
		else
		{
			echo 'Database tables have been successfully installed. Version: '.$xml_vers.'<br /><br />';
			echo 'Post Installation script run completed successfully';
			return true;
		}		
	}
	else
	{
		if ($xml_vers == $db_vers)
		{
			// All is ok. Nothing more to do
			echo 'JV-BE database tables are the latest and are in sync with the installed JV-BE files<br /><br />';
			echo 'Post Installation script run completed successfully';
			return true;
		}
		else if ($xml_vers < $db_vers) 
		{
			// XML file has lower version than DB! Ask the user to install the correct version
			echo 'JV-BE database tables seem to be corresponding to a version higher than the JV-BE files that you currently have installed<br />';
			echo 'Post installation task failed. Please contact support@www.jv-extensions.com';
			return false;			
		}
		else
		{
			// Database needs to be upgraded to match the latest component installation
			echo 'Initiating an upgrade of the database tables to latest version...<br />';			
			while ($db_vers < $xml_vers)
			{
				$db_vers++;
				if (!Jvlinx_PostInstall::execUpgrade($db_vers, $retmsg))
				{
					echo $retmsg . 'Error while upgrading to database version: '.$db_vers.'<br />';
					echo 'Post installation task failed. Please contact support@www.jv-extensions.com';
					return false;
				}

				echo 'Database tables have been successfully upgraded to version: '.$db_vers.'<br />';		
			}
			
			echo "Post Installation script run completed successfully";
			return true;
		}
	}	
	
    return "Installation successful! Please visit <a href='http://www.jv-extensions.com' target='_blank'>www.jv-extensions.com</a> for latest product NEWS, upgrades & support."; 
} 
?>